<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Write extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		
		$this->editArticleID = 0;
	}

	function index()
	{
        if(!$this->session->current_user())
        {
            redirect('login/from/write');
        }
        
		$articleID = $this->editArticleID;
		$this->editArticleID = 0;
		
		if($articleID)
		{
			$this->oxarticle->load($articleID);
		}
		else
		{
			$this->oxarticle->clear($this->session->current_user());
		}
	
		$data['article'] = $this->oxarticle;
		
		$data2['boxes'] = array (
		         array ("style"=>"", "title"=>"Info",
						"text"=> '<h1>Make your article more useful!</h1>'.
										'<b>Perform following optional steps to make your article easier to find to those who would like to read it:</b>.'.
										'<a>(Click on each step to learn more about it.)</a>.'.
										'<a>Write an abstract.</a>'.
										'<a>Select geo stamp! At least one.</a>'.
										'<a>Select tags that well define your article.</a>'.
										'<a>Select time stamp! At least one.</a>'.
										'<a>Add your article to an existing story.</a>'.
										'<a>Select a prequel or a background article.</a>'.
										'<a>Have your article endorsed by another user.</a>'.
										'<a>Let the world know about your article: Post it to facebook, Send a Tweet about it, EMail your friends and contacts.</a>'),
		         array ("style"=>"", "title"=>"Tips",
						"text"=> 'Tip: make you article more visible'. 
										'Let someone endorse your article and it has boosts score'.
										'You can improve your article later, speed also matters'.
										'You will get higher score if you authenticate your account.'));
	
		$this->ShowHeader('', array('datepicker.js','write.js'), array('datepicker.css'));
		$this->load->view('write_page'  , $data);
		$this->load->view('write_page2'  , $data);
		$this->ShowFooter();
	}
	
	private $editArticleID;
	
	function edit()
	{
		$this->editArticleID = $this->uri->segment(3);
		$this->index();
	}
	
	function dowrite()
	{
		redirect('news');
	}
	
	function save()
	{
		//Check if the current cache object is the same as the one retrieved
		
		//Update current cache object
		$this->oxarticle->update($_POST);
		
		$this->oxarticle->save();
		
		//Return value will be the article ID.
		echo '       ';
		echo $this->oxarticle->idarticle;
	}
	
	//button handlers
	function add_tag()
	{
		$data['article_id'] = '';
		$data['criteria'] = '';
        $data['offset'] = 0;
		$data['show_all_available'] = false;
		if(isset($_POST['offset']))        
        {
            $data['offset'] = $_POST['offset'];
        }		
        if(isset($_POST['article_id']))        
        {
            $data['article_id'] = $_POST['article_id'];
        }
        else
        {
            exit('No direct script access allowed');
        }
		if(isset($_POST['criteria']))
		{
			$data['criteria'] = $_POST['criteria'];
			if(isset($_POST['show_all']) && ($_POST['show_all'] != false))
			{
				$data['tags'] = $this->tags->get_tags($data['criteria']);
			}
			else
			{
				$data['tags'] = $this->tags->get_tags($data['criteria'], 20, $data['offset']);
				$data['show_all_available'] = true;
			}
		}
		else
		{
			$data['tags'] = $this->tags->get_random_tags(20, $data['offset']);
		}
		
		$this->load->view('popup/add_tag'  , $data);
	}
	
	function add_this_tag()
	{
        $this->oxarticle->idarticle = $this->uri->segment(3);
        $newTagId = $this->uri->segment(4);
        
        $this->oxarticle->add_tag($newTagId);
        redirect('write/edit/'.$this->oxarticle->idarticle);        
	}
	function remove_this_tag()
	{
            $this->oxarticle->idarticle = $this->uri->segment(3);
            $tagToRemove = $this->uri->segment(4);

            $this->oxarticle->remove_tag($tagToRemove);
            redirect('write/edit/'.$this->oxarticle->idarticle);        
	}   
    
    function create_new_tag()
    {
        $this->oxarticle->idarticle = $this->uri->segment(4);
        $newTagName = urldecode($this->uri->segment(3));

        $newTagId = $this->tags->create_new($newTagName);
        
        $this->oxarticle->add_tag($newTagId);
        redirect('write/edit/'.$this->oxarticle->idarticle);         
    }
    //people tags
	function add_peopletag()
	{
		$data['article_id'] = '';
		$data['criteria'] = '';
        $data['offset'] = 0;
		$data['show_all_available'] = false;
		if(isset($_POST['offset']))        
        {
            $data['offset'] = $_POST['offset'];
        }		
        if(isset($_POST['article_id']))        
        {
            $data['article_id'] = $_POST['article_id'];
        }
        else
        {
            exit('No direct script access allowed');
        }
		if(isset($_POST['criteria']))
		{
			$data['criteria'] = $_POST['criteria'];
			if(isset($_POST['show_all']) && ($_POST['show_all'] != false))
			{
				$data['peopletags'] = $this->peopletags->get_peopletags($data['criteria']);
			}
			else
			{
				$data['peopletags'] = $this->peopletags->get_peopletags($data['criteria'], 20, $data['offset']);
				$data['show_all_available'] = true;
			}
		}
		else
		{
			$data['peopletags'] = $this->peopletags->get_random_tags(20, $data['offset']);
		}
		
		$this->load->view('popup/add_peopletag'  , $data);
	}
	
	function add_this_peopletag()
	{
        $this->oxarticle->idarticle = $this->uri->segment(3);
        $newPeopleTagId = $this->uri->segment(4);
        
        $this->oxarticle->add_peopletag($newPeopleTagId);
        redirect('write/edit/'.$this->oxarticle->idarticle);        
	}
	function remove_this_peopletag()
	{
            $this->oxarticle->idarticle = $this->uri->segment(3);
            $peopleTagToRemove = $this->uri->segment(4);

            $this->oxarticle->remove_peopletag($peopleTagToRemove);
            redirect('write/edit/'.$this->oxarticle->idarticle);        
	}   
    
    function create_new_peopletag()
    {
        $this->oxarticle->idarticle = $this->uri->segment(4);
        $newPeopleTagName = urldecode($this->uri->segment(3));

        $newPeopleTagId = $this->peopletags->create_new($newPeopleTagName);
        
        $this->oxarticle->add_peopletag($newPeopleTagId);
        redirect('write/edit/'.$this->oxarticle->idarticle);         
    }
        //Geo location specifying
	function add_geostamp()
	{
            $data['article_id'] = '';
            $data['criteria'] = '';
            $data['offset'] = 0;
            $data['show_all_available'] = false;
            if(isset($_POST['offset']))        
            {
                $data['offset'] = $_POST['offset'];
            }		
            if(isset($_POST['article_id']))        
            {
                $data['article_id'] = $_POST['article_id'];
            }
            else
            {
                exit('No direct script access allowed');
            }
            if(isset($_POST['criteria']))
            {
                $data['criteria'] = $_POST['criteria'];
                if(isset($_POST['show_all']) && ($_POST['show_all'] != false))
                {
                        $data['geostamps'] = $this->geodata->get_geostamps($data['criteria']);
                }
                else
                {
                        $data['geostamps'] = $this->geodata->get_geostamps($data['criteria'], 20, $data['offset']);
                        $data['show_all_available'] = true;
                }
            }
            else
            {
                $data['geostamps'] = array();
                    //$data['geostamps'] = $this->geodata->get_top_geostamps(20, $data['offset']);
            }
            $this->load->view('popup/add_geolocation'  , $data);
	}
	
	function add_this_geostamp()
	{
        $this->oxarticle->idarticle = $this->uri->segment(3);
        $newGeoNamId = $this->uri->segment(4);
        
        $this->oxarticle->add_geolocation($newGeoNamId);
        redirect('write/edit/'.$this->oxarticle->idarticle);        
	}
	function remove_this_geostamp()
	{
        $this->oxarticle->idarticle = $this->uri->segment(3);
        $geoNamIdToRemove = $this->uri->segment(4);

        $this->oxarticle->remove_geolocation($geoNamIdToRemove);
        redirect('write/edit/'.$this->oxarticle->idarticle);        
	}        
	function add_links()
	{
            $data['article_id'] = '';
            if(isset($_POST['article_id']))        
            {
                $data['article_id'] = $_POST['article_id'];
            }
            else
            {
                exit('No direct script access allowed');
            }
            $this->load->view('popup/add_link'  , $data);
	}
	
	function add_new_link()
	{
        $id = $_POST['article_id'];
        $link_url = $_POST['link_url'];
        $link_text = $_POST['link_text'];
        
        $this->oxarticle->idarticle = $id;
        
        $this->oxarticle->add_new_link($link_url, $link_text);
	}
    
	function remove_this_link()
	{
        $this->oxarticle->idarticle = $this->uri->segment(3);
        $link_article_id_to_remove = $this->uri->segment(4);

        $this->oxarticle->remove_link($link_article_id_to_remove);
        redirect('write/edit/'.$this->oxarticle->idarticle);        
	}  
    
    function add_document()
    {
            $data['article_id'] = '';
            if(isset($_POST['article_id']))        
            {
                $data['article_id'] = $_POST['article_id'];
            }
            else
            {
                exit('No direct script access allowed');
            }
            $this->load->view('popup/add_document' , $data);        
    }
    ///////////////////////////////////////////////////////////////////////
    // Adding a related article
    function add_related_article()
	{
            $data['article_id'] = '';
            $data['criteria'] = '';
            $data['offset'] = 0;
            $data['show_all_available'] = false;
            if(isset($_POST['offset']))        
            {
                $data['offset'] = $_POST['offset'];
            }		
            if(isset($_POST['article_id']))        
            {
                $data['article_id'] = $_POST['article_id'];
            }
            else
            {
                exit('No direct script access allowed');
            }
            if(isset($_POST['criteria']))
            {
                $data['criteria'] = $_POST['criteria'];
                if(isset($_POST['show_all']) && ($_POST['show_all'] != false))
                {
                        $data['articles'] = $this->articles->search_articles($data['criteria']);
                }
                else
                {
                        $data['articles'] = $this->articles->search_articles($data['criteria'], 3, $data['offset']);
                        $data['show_all_available'] = true;
                }
            }
            else
            {
                $data['articles'] = array();
                    //$data['geostamps'] = $this->geodata->get_top_geostamps(20, $data['offset']);
            }
            $this->load->view('popup/add_related_articles'  , $data);
	}
	
	function add_new_related_article()
	{
        $id = $_POST['article_id'];
        $article_relation_type = $_POST['article_relation_type'];
        $related_article_id = $_POST['related_article_id'];
        
        $this->oxarticle->idarticle = $id;
        
        $this->oxarticle->add_new_related_article($article_relation_type, $related_article_id);
	}
	function remove_related_article()
	{
        $this->oxarticle->idarticle = $this->uri->segment(3);
        $articleIdToRemove = $this->uri->segment(4);

        $this->oxarticle->remove_related_article($articleIdToRemove);
        redirect('write/edit/'.$this->oxarticle->idarticle);        
	}
    
    ////////////////////////////////////////////////////////////////////////////
    // Add to story
    
    function add_to_story()
	{
        $data['article_id'] = '';
        $data['criteria'] = '';
        $data['offset'] = 0;
        $data['show_all_available'] = false;
        if(isset($_POST['offset']))        
        {
            $data['offset'] = $_POST['offset'];
        }		
        if(isset($_POST['article_id']))        
        {
            $data['article_id'] = $_POST['article_id'];
        }
        else
        {
            exit('No direct script access allowed');
        }
        if(isset($_POST['criteria']))
        {
            $data['criteria'] = $_POST['criteria'];
            if(isset($_POST['show_all']) && ($_POST['show_all'] != false))
            {
                    $data['articles'] = $this->articles->search_articles($data['criteria']);
            }
            else
            {
                    $data['articles'] = $this->articles->search_articles($data['criteria'], 3, $data['offset']);
                    $data['show_all_available'] = true;
            }
        }
        else
        {
            $data['articles'] = array();
        }
        $this->load->view('popup/add_to_story'  , $data);
	}
	function add_to_this_story()
	{
        $id = $_POST['article_id'];
        $related_article_id = $_POST['related_article_id'];
        
        $this->oxarticle->idarticle = $id;
        
        $this->oxarticle->add_to_this_story($related_article_id);
	}    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */